package com.db.collections;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.TreeMap;

/**
 * @author nulls.narik
 *         Date: 21.05.13
 *         Time: 16:52
 */
public class TestMemoryConsumption extends TestCollections {

    public static void main(String[] args) {
        TestMemoryConsumption testMemoryConsumption = new TestMemoryConsumption();
        testMemoryConsumption.test();
    }

    @Override
    public void test() {
        int n = 100000;

        long memory = 0, memoryArray = 0, memoryHashMap = 0, memoryTreeMap = 0, memoryLinkedList = 0, memoryArrayList = 0;
        int[] array;
        HashMap<Integer, Integer> hashMap;
        TreeMap<Integer, Integer> treeMap;
        LinkedList<Integer> linkedList;
        ArrayList<Integer> arrayList;

        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = i;
        }
        memoryArray = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memory;

        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        hashMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            hashMap.put(i, i);
        }
        memoryHashMap = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memory;

        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        treeMap = new TreeMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            treeMap.put(i, i);
        }
        memoryTreeMap = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memory;


        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        linkedList = new LinkedList<Integer>();
        for (int i = 0; i < n; i++) {
            linkedList.add(i);
        }
        memoryLinkedList = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memory;


        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        arrayList = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            arrayList.add(i);
        }
        memoryArrayList = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memory;









        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += array[i];
        }
        sum = sum / n;
        System.out.println(String.format("Avr: %d", sum));

        sum = 0;
        for (int i = 0; i < n; i++) {
            sum += hashMap.get(i);
        }
        sum = sum / n;
        System.out.println(String.format("Avr: %d", sum));

        sum = 0;
        for (int i = 0; i < n; i++) {
            sum += treeMap.get(i);
        }
        sum = sum / n;
        System.out.println(String.format("Avr: %d", sum));

        sum = 0;
        for (int i = 0; i < n; i++) {
            sum += linkedList.get(i);
        }
        sum = sum / n;
        System.out.println(String.format("Avr: %d", sum));

        sum = 0;
        for (int i = 0; i < n; i++) {
            sum += arrayList.get(i);
        }
        sum = sum / n;
        System.out.println(String.format("Avr: %d", sum));


        System.out.println(String.format("Memory array: %d {%.2f}; memory HashMap: %d [%.2f] {%.2f}; memory TreeMap: %d [%.2f] {%.2f};" +
                " memory LinkedList: %d [%.2f] {%.2f}; memory ArrayList: %d [%.2f] {%.2f}",
                memoryArray, (float) memoryArray / n,
                memoryHashMap, (float) memoryHashMap / memoryArray, (float) memoryHashMap / n,
                memoryTreeMap, (float) memoryTreeMap / memoryArray, (float) memoryTreeMap / n,
                memoryLinkedList, (float) memoryLinkedList / memoryArray, (float) memoryLinkedList / n,
                memoryArrayList, (float) memoryArrayList / memoryArray, (float) memoryArrayList / n));
    }
}
